<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="70%"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="编号">
        <el-input v-model.trim="form.sn" :disabled="true" show-word-limit />
      </el-form-item>
      <el-form-item label="模块" prop="type">
        <el-select v-model="form.type">
          <el-option label="新闻动态" value="1" />
          <el-option label="通知公告" value="2" />
          <el-option label="实时热点" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model.trim="form.title" maxlength="200" show-word-limit />
      </el-form-item>
      <el-form-item label="内容">
        <vab-quill
          v-model="form.content"
          :min-height="200"
          :options="options"
        />
      </el-form-item>
      <el-form-item label="权限" prop="role">
        <el-radio v-model="form.role" label="-1">全部</el-radio>
        <el-radio v-model="form.role" label="1">管理员</el-radio>
        <el-radio v-model="form.role" label="2">普通用户</el-radio>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio v-model="form.status" label="1">正常</el-radio>
        <el-radio v-model="form.status" label="2">禁用</el-radio>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import VabQuill from '@/extra/VabQuill'
  import { doEdit } from '@/api/docManagement'
  import { formatDate, random } from '@/utils/index'
  export default {
    name: 'MarkdownEditor',
    components: { VabQuill },
    data() {
      return {
        dialogFormVisible: false,
        title: '',
        form: {
          type: '1',
          title: '',
          role: '-1',
          content: '请输入发布的内容',
          status: '1',
        },
        rules: {
          title: [
            { required: true, trigger: 'blur', message: '请输入标题' },
            {
              min: 1,
              max: 200,
              message: '标题长度在 1 到 200 个字符',
              trigger: 'blur',
            },
          ],
          role: [{ required: true, trigger: 'blur', message: '请选择权限' }],
          status: [{ required: true, trigger: 'blur', message: '请选择状态' }],
          type: [{ required: true, trigger: 'blur', message: '请选择模块' }],
        },
        options: {
          theme: 'snow',
          bounds: document.body,
          debug: 'warn',
          modules: {
            toolbar: {
              container: [
                ['bold', 'italic', 'underline', 'strike'],
                [{ header: [1, 2, 3, 4, 5, 6, false] }],
                [{ size: ['small', false, 'large', 'huge'] }],
                [{ color: [] }, { background: [] }],
                ['blockquote', 'code-block'],
                [{ list: 'ordered' }, { list: 'bullet' }],
                [{ script: 'sub' }, { script: 'super' }],
                [{ indent: '-1' }, { indent: '+1' }],
                [{ align: [] }],
                [{ direction: 'rtl' }],
                [{ font: [] }],
                ['link'],
              ],
              handlers: {
                'vab-upload-image': () => {
                  this.$baseConfirm(
                    '演示环境未使用真实文件服务器，故图片上传回显不会生效，开发时请修改为正式文件服务器地址',
                    '开发注意事项！！！',
                    () => {
                      this.$refs['vabUpload'].handleShow()
                    },
                    () => {
                      this.handleAddImg()
                    },
                    '模拟打开文件上传',
                    '模拟添加一张文件服务器图片'
                  )
                },
              },
            },
          },
          placeholder: '请输入内容',
          readOnly: false,
        },
      }
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '发布'
          var date = new Date()
          this.form.content = '请输入发布的内容'
          this.form.sn =
            'V' + formatDate(date, 'yyyyMMddhhmmss') + random(1000, 9999)
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          this.form.status = this.form.status + ''
          this.form.role = this.form.role + ''
          this.form.content = row.content
          this.form.type = row.type + ''
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            console.log(this.form)
            const { msg } = await doEdit(this.form)
            this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
